# TODO: 导入必要的库和模块
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
import pickle
from tqdm import tqdm
import warnings
warnings.filterwarnings('ignore')

# TODO: 加载数字数据集
digits = load_digits()
X = digits.data
y = digits.target

# TODO: 将数据集划分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# TODO: 初始化变量以存储最佳准确率，相应的k值和最佳knn模型
best_accuracy = 0
best_k = 0
best_knn_model = None

# TODO: 初始化一个列表以存储每个k值的准确率
accuracies = []

# TODO: 尝试从1到40的k值，对于每个k值，训练knn模型，保存最佳准确率，k值和knn模型
k_range = range(1, 41)

for k in tqdm(k_range, desc="Searching for optimal K"):
    # 创建KNN分类器
    knn = KNeighborsClassifier(n_neighbors=k)
    
    # 训练模型
    knn.fit(X_train, y_train)
    
    # 预测测试集
    y_pred = knn.predict(X_test)
    
    # 计算准确率
    accuracy = accuracy_score(y_test, y_pred)
    accuracies.append(accuracy)
    
    # 更新最佳模型
    if accuracy > best_accuracy:
        best_accuracy = accuracy
        best_k = k
        best_knn_model = knn

# TODO: 将最佳KNN模型保存到二进制文件
with open('best_knn_model.pkl', 'wb') as f:
    pickle.dump(best_knn_model, f)

# TODO: 打印最佳准确率和相应的k值
print(f"\n最佳 K 值: {best_k}")
print(f"最佳准确率: {best_accuracy:.4f} ({best_accuracy*100:.2f}%)")

# 绘制准确率随K值变化的图表
plt.figure(figsize=(12, 6))
plt.plot(k_range, accuracies, 'b-', linewidth=2, label='准确率')
plt.axvline(x=best_k, color='red', linestyle='--', linewidth=2, label=f'最佳 K={best_k}')

# 标记最佳点
plt.plot(best_k, best_accuracy, 'ro', markersize=8)
plt.text(best_k + 0.5, best_accuracy - 0.02, 
         f'K={best_k}\n准确率={best_accuracy:.4f}', 
         fontsize=10, bbox=dict(boxstyle="round,pad=0.3", facecolor="white", alpha=0.8))

plt.xlabel('K 值')
plt.ylabel('准确率')
plt.title('KNN 分类器在不同 K 值下的准确率')
plt.legend()
plt.grid(True, alpha=0.3)
plt.xticks(range(1, 41, 2))

# 保存图表
plt.savefig('accuracy_plot.pdf', format='pdf', bbox_inches='tight')
plt.show()